package cate.game.pvp.throne.data.ko;

import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;


@NoteClass("王座争霸-单个分组的-淘汰赛排行榜")
@Slf4j
public class TBKoRank {

	@NoteField( "排行榜列表")
	public List<TBKoRankItem> rankList = new ArrayList<>();

	public void initialize() {
		if (rankList == null) {
			rankList = new ArrayList<>();
		}
	}

	public synchronized void sort() {
		// 胜场 回合数 选拔赛积分 战力
		rankList.sort(Comparator.comparing((TBKoRankItem a) -> -a.win)
				.thenComparing(a -> -a.round)
				.thenComparing(a -> -a.owner.power));
	}


	/** 分组后的记录 */
	public synchronized void record(List<TBKoRankItem> rankList) {
		this.rankList.clear();
		this.rankList.addAll(rankList);
	}


	/**
	 * 根据唯一ID获取对应的排名 (排名从1开始)
	 *
	 * @return -1表示没有进入排名
	 */
	public int findRankByUid(String roleUid) {
		for (int i = 0; i < rankList.size(); i++) {
			TBKoRankItem item = rankList.get(i);
			if (StringUtils.equals(item.owner.uid, roleUid)) {
				return i + 1;
			}
		}
		return -1;
	}

	public TBKoRankItem findByUid(String roleUid) {
		for (TBKoRankItem item : rankList) {
			if (StringUtils.equals(item.owner.uid, roleUid)) {
				return item;
			}
		}
		return null;
	}

	public List<TBKoRankItem> getRankList() {
		return rankList;
	}

//	/**
//	 * 结算奖励
//	 * @param groupType
//	 * @param levelGroup
//	 */
//	public void handleRwd(int groupType, int levelGroup){
//		for (int i = 0; i < list.size(); i++) {
//			PeakKoRankItemPO item = list.get(i);
//			if(!item.owner.srv.isLocal()){
//				continue;
//			}
//			int rank = i+1;
//			MixRes reward = Game.config.peak.rank.findRwd(groupType, levelGroup, rank);
//			if (reward != null) {
//				List<Object> params = new ArrayList<>();
//				params.add(rank);
//				MailPO mail =
//						new MailBuilder()
//								.setReward(reward)
//								.setContentId(GDMail.Tid.PEAK_RANK_RWD)
//								.setParamList(params)
//								.setOperation(GDOperation.PEAK_RANK_RWD)
//								.setOperationSub(rank)
//								.build();
//				Game.mail.send(item.owner._idHex, mail);
//			}
//		}
//	}

	public void clear() {
		this.rankList.clear();
	}

	public TBKoRankItem getChampion() {
		if (rankList.isEmpty()) {
			return null;
		}
		return rankList.get(0);
	}

}
